<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="连接名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelName">
              <a-input v-model="model.channelName" placeholder="请输入连接名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="连接代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelName">
              <a-input v-model="model.channelCode" placeholder="请输入连接代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if='model.channelType === "TCP_SERVER"'>
            <a-form-model-item label="本机端口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelName">
              <a-input v-model="model.channelLocalPort" placeholder="请输入本机端口"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="连接类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelName">
              <a-select v-model="model.channelType">
                <a-select-option value="TCP_CLIENT"> tcp客户端 </a-select-option>
                <a-select-option value="TCP_SERVER"> tcp服务端 </a-select-option>
                <a-select-option value="UDP_UNICAST"> 单播 </a-select-option>
                <a-select-option value="UDP_MULTICAST"> 多播 </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="远程IP" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelHost">
              <a-input v-model="model.channelHost" placeholder="请输入远程IP"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="远程端口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelPort">
              <a-input-number v-model="model.channelPort" placeholder="请输入远程端口" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="大小端" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="byteOrder">
              <a-select v-model="model.byteOrder">
                <a-select-option value="BIG_END">大端</a-select-option>
                <a-select-option value="SMALL_END">小端</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="初始化类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="initClassName">
              <a-input v-model="model.initClassName" placeholder="请输入初始化类名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启动顺序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startOrder">
              <a-input-number v-model="model.startOrder" placeholder="请输入启动顺序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="连接来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelPort">-->
<!--              <j-dict-select-tag v-model="model.channelServerType" dictCode="channel_source" placeholder="请选择" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="帧长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="allowFrameLength">-->
<!--              <a-input v-model="model.allowFrameLength" placeholder="请输入帧长" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="帧头" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="frameHeader">-->
<!--              <a-input v-model="model.frameHeader" placeholder="请输入帧头"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <!-- <a-col :span="24">
            <a-form-model-item
              label="启动状态 on:启动 off:关闭"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="status"
            >
              <a-input v-model="model.status" placeholder="请输入启动状态 on:启动 off:关闭"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="0创建时启动，1使用时启动"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="lazy"
            >
              <a-input-number v-model="model.lazy" placeholder="请输入0创建时启动，1使用时启动" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'NmcChannelInfoForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      channelTypeList: [
        { label: 'tcp客户端', value: 'TCP_CLIENT' },
        { label: 'tcp服务端', value: 'TCP_SERVER' },
        { label: '单播', value: 'UDP_UNICAST' },
        { label: '多播', value: 'UDP_MULTICAST' },
      ],
      url: {
        add: '/channel/nmcChannelInfo/add',
        edit: '/channel/nmcChannelInfo/edit',
        queryById: '/channel/nmcChannelInfo/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model.channelLocalHost = '0.0.0.0'
          this.model.channelLocalPort = '0'
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
